package org.jeecg.modules.flow.callback.inbound.other;

import cn.hutool.core.util.StrUtil;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.business.transfer.entity.PaperPurchase;
import org.jeecg.modules.business.transfer.service.IPaperPurchaseService;
import org.jeecg.modules.business.transfer.vo.PaperPurchasePage;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 纸筒采购任务编辑和保存回调前bean
 *
 * @author lumingyi
 * @version 1.0.0
 * @since 2023-11-14
 */
@Component
public class R47PaperPurchaseSaveOrUpdateBeforeBean<T> extends BaseCustomBeanRun<T> {
    @Autowired
    private IPaperPurchaseService paperPurchaseService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void actureRun() {
        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        // 转class.自定义class,data对应的class
        baseFlowDto.translateClass(PaperPurchasePage.class);
        PaperPurchasePage data = (PaperPurchasePage) baseFlowDto.getData();
        PaperPurchase paperPurchase = new PaperPurchase();
        BeanUtils.copyProperties(data, paperPurchase);
        if (StrUtil.isNotBlank(data.getId())) {
            paperPurchaseService.updateMain(paperPurchase, data.getWmsPaperPurchaseMaterialList());
            baseFlowDto.setResult(Result.OK("编辑成功"));
        } else {
            String id = paperPurchaseService.saveMain(paperPurchase, data.getWmsPaperPurchaseMaterialList());
            // id是保存业务记录的id。用于生成业务流程信息记录
            baseFlowDto.setInfoId(id);
            baseFlowDto.setResult(Result.OK(id));
        }
    }
}
